<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Client Side Pagination in DataGrid - jQuery EasyUI Demo</title>

</head>
<body>
	<div id="tb4" ></div>
	<div id="dlg4" class="easyui-dialog" title="配置信息" style="padding:10px"
			data-options="				
				iconCls: 'icon-edit',
				modal : true,
				closed : true,
				width : 400,
				buttons: [{
					text:'确定',
					handler:function(){
						var keyTy = $('#keyTy').val();
						if (keyTy == ''){
							$.messager.alert('缺少参数','关键字不能为空','error'); 
							return;
						}
						var cd = $('#cd').val();
						var decode = $('#decode').val();
						var lastUpdateId = $('#lastUpdateId').val();
						$.ajax({
							type : 'POST', 
							url : '/updateSystemInfo',
							data : { 'keyTy': keyTy , 'cd': cd , 'decode' : decode, 'lastUpdateId' : lastUpdateId },
							success : function(result) {
								if ( result ) {
									$.messager.show({
										title:'成功',
										msg:'更新系统配置成功',
										timeout:2000,
										showType:'slide'
									});
									$('#tb4').datagrid('reload');
								} else { 
									$.messager.show({
										title:'失败',
										msg:'更新系统配置失败',
										timeout:2000,
										showType:'slide'
									}); 
								} 
							},
							error : function(){
						       	$.messager.show({
									title:'失败',
									msg:'更新系统配置失败，请重试',
									timeout:2000,
									showType:'slide'
								}); 
							}
						});
						$('#dlg4').dialog('close');
					}
				},{
					text:'取消',
					handler:function(){
						$('#dlg4').dialog('close');
					}
				}]
	">
		<table>
			<tr>
				<td>关键字:</td>
				<td><input id="keyTy" disabled="disabled" type="text" style="width: 300px"></td>
			</tr>			
			<tr>
				<td>值:</td>
				<td><input id="cd"  type="text" style="width: 300px"></td>
			</tr>			
			<tr>
				<td>描述:</td>
				<td><input id="decode" type="text" style="width: 300px"></td>
			</tr>
			<tr>
				<td>更新者:</td>
				<td><input id="lastUpdateId" disabled="disabled" type="text" style="width: 300px"></td>
			</tr>
		</table>
	</div>
	<script type="text/javascript" th:inline="none">
		var row4;
	
		$('#tb4').datagrid({
			border:false,
			fit:true,
			singleSelect:true,
			autoRowHeight:false,
			remoteSort: false,
			url:'/getSystemInfo',
			method:'post',
			toolbar: [{
				text:'添加',
				iconCls:'icon-add',
				handler:function(){
					$('#keyTy').val('');
					$("#keyTy").removeAttr("disabled");
					$('#cd').val('');
					$('#decode').val('');
					$('#lastUpdateId').val('szd');
					row4 = null;
					$('#dlg4').dialog('open');
				}
			},{
				text:'编辑',
				iconCls:'icon-edit',
				handler:function(){
					row4 = $('#tb4').datagrid('getSelected');
					if(row4!=null){
						var keyTy = row4.keyTy;
						var cd = row4.cd;
						var decode = row4.decode;
						var lastUpdateId = row4.lastUpdateId;
						
						$("#keyTy").val(keyTy);
						$('#keyTy').attr('disabled','disabled');
						$("#cd").val(cd);
						$("#decode").val(decode);
						$("#lastUpdateId").val(lastUpdateId);
						
						$('#dlg4').dialog('open');
						
						row4 = null;
					}
					else{
						$.messager.show({
							title:'错误',
							msg:'请先选择一行',
							timeout:2000,
							showType:'slide'
						});
					}
				}
			},{
				text:'删除',
				iconCls:'icon-remove',
				handler:function(){
					row4 = $('#tb4').datagrid('getSelected');
					if(row4!=null){
						var keyTy = row4.keyTy;
						$.messager.confirm('请确认', '将删除配置'+keyTy+'，确认吗？', function(r){
							if (r){
								$.ajax({
								    type : "POST", 
								    url : "/deleteSystemInfo",
								    data : { "keyTy": keyTy },
								    success : function(result) {
								    	if(result){
									       	$.messager.show({
												title:'结果',
												msg: '删除配置'+keyTy+'成功',
												timeout:3000,
												showType:'slide'
											});
									       	$('#tb4').datagrid('reload');
								    	}
								    	else{
									       	$.messager.show({
												title:'结果',
												msg: '删除配置'+keyTy+'失败',
												timeout:3000,
												showType:'slide'
											});
								    	}
								    },
									error : function(e){
								       	$.messager.show({
											title:'结果',
											msg:'删除配置'+keyTy+'失败，这可能是由于服务器出现问题引起的 '+e,
											timeout:2000,
											showType:'slide'
										}); 
									}
								});
							}
						});
						row4 = null;
					}
					else{
						$.messager.show({
							title:'错误',
							msg:'请选择一行',
							timeout:2000,
							showType:'slide'
						});
					}
				}
			},{
				text:'刷新',
				iconCls:'icon-reload',
				handler:function(){
					$('#tb4').datagrid('reload');
				}
			},{
				text:'恢复默认值',
				iconCls:'icon-redo',
				handler:function(){
					$.messager.confirm('重置', '将重置除GLOBAL_REG之外所有的系统变量为默认值，确认吗', function(r){
						if (r){
							$.ajax({
							    type : "POST", 
							    url : "/resetSystemInfo",
							    success : function(result) {
							    	if(result){
								      	$.messager.show({
											title:'结果',
											msg: '重置成功',
											timeout:2000,
											showType:'slide'
										});
								      	$('#tb4').datagrid('reload');
							    	}
							    	else{
								      	$.messager.show({
											title:'结果',
											msg: '重置失败',
											timeout:2000,
											showType:'slide'
										});
							    	}
							    },
							    error : function(e){
							       	$.messager.show({
										title:'失败',
										msg:'重置失败，请假查服务器',
										timeout:2000,
										showType:'slide'
									}); 
							    }
							});
						}
					});
				}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){
					$.messager.alert('帮助','要从CF获取你的域名信息，请先配置CF_AUTH_EMAIL和CF_AUTH_EMAIL，其余项可以按需填写','');
				}
			}],
			columns : [[
				{field : 'keyTy',title : '关键字',align : 'center',sortable:true}, 
				{field : 'cd',title : '值',align : 'center',sortable:true}, 
				{field : 'decode',title : '描述',align : 'center',sortable:true}, 
				{field : 'lastUpdateId',title : '更新者',align : 'center',sortable:true},
			]],
            onLoadError: function (data){
            	console.log(data);
            	$.messager.alert('错误', data.responseText ,'error');
				$('#tb4').datagrid('loadData',{'total':0,'rows':[]});
            }
		});
	</script>
</body>
</html>
